package dom.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;

import dom.bean.WineC;
import dom.util.Connections;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
/**
 * Servlet implementation class 
 */

public class WineCatg extends HttpServlet {
    private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public WineCatg() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        //response.getWriter().append("Served at: ").append(request.getContextPath());
    	com.mysql.jdbc.Connection con=Connections.getConnection();
        PreparedStatement  pstmt=null;
        ResultSet rs=null;
		String sql="select * from productcat";
		ArrayList<WineC> list = new ArrayList<WineC>();
        try {
			pstmt= con.prepareStatement(sql);
			rs=pstmt.executeQuery(); 
			rs.beforeFirst();
			while(rs.next()) {
				WineC w = new WineC();
				w.setID(rs.getInt("id"));
				w.setTitle(rs.getString("title"));
				list.add(w);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
        Connections.closeDB(con, pstmt, rs);
    	
    	
    	request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        
        response.setContentType("text/html;charset=utf-8");

        JSONObject json = new JSONObject();
        //将ArrayList对象转化为JSON对象，因为Echarts前台只能识别JSONArray，不能识别ArrayList类型

        json.put("code", 0);
        json.put("msg", "");
        json.put("count",1000);
        json.put("data", list);
//        String Sjson = json.toJSONString(list);
        //传回jsp
        response.getWriter().append(json.toString());
   
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }

}